<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';


define('DBPAS',"coolchuan2012");
//define('DBPAS',"123456");

$link = mysql_connect("localhost","root",DBPAS); 
//$link = mysql_connect("localhost","root","coolchuan2012"); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('ONCECATCHCOUNT',15);
define('ONCECOMMENTCATCHCOUNT',50);

define('WEBURLHEADERANZHI','http://www.anzhi.com/soft_');
define('WAPURLHEADERANZHI','http://m.anzhi.com/info_');

define('WEBURLHEADERBAIDU','http://as.baidu.com/a/item?docid=');
define('WAPURLHEADERBAIDU','http://as.baidu.com/a/item?docid=');
define('WAPURLFOOTERBAIDU','&pre=web_am_software&pos=software_1012_0&f=suite_3%40software_1012_0&suite=3&bdtype=soft&appname=soft&version=2.2');

define('WEBURLHEADERGFAN','http://apk.gfan.com/Product/App');
define('WAPURLHEADERGFAN','http://3g.gfan.com/data/index.php?/detail/ajaxdetail/');


define('WAPURLHEADER360','http://openboxcdn.mobilem.360.cn/mintf/getAppInfoByIds?pname=');


define('WAPURLHEADERWANDOUJIA','http://apps.wandoujia.com/api/v1/apps/');
define('WAPURLFOOTERWANDOUJIA','?f=phoenix2&v=3.34.1&u=ecaf666a0f8e4553b843d5e61e80c62ad30247cd&max=20&start=0&opt_fields=apks.compatible%2Capks.incompatibleDetail%2Cad%2Capks.adsType%2Capks.beta%2Capks.bytes%2Capks.downloadUrl.*%2Capks.size%2Capks.versionCode%2Capks.versionName%2Capks.securityStatus%2Capks.md5%2Capks.permissionLevel%2Capks.superior%2Capks.dangerousPermissions%2Capks.permissions%2Capks.securityDetail.*%2Capks.resolution%2Ccategories.alias%2Ccategories.name%2Cdescription%2Ctitle%2CcommentsCount%2ClikeCount%2CdislikesCount%2CinstalledCountStr%2Cicons.px78%2CpackageName%2Cid%2ClikesRate%2Cscreenshots.*%2Capks.verified%2Capks.paidType%2CimprUrl%2Cbanner%2Ctags.*&vc=4197&ch=wandoujia_wap&pos=m/tag/shopping');


//define('LOGPATHHEADER','/usr/share/nginx/www/');
define('LOGPATHHEADER','');

define('LOGFORSUCCESS',0);

$packagename = $_GET["packagename"];
$id360 = $_GET["id360"];
$idwandoujia = $_GET["idwandoujia"];
$idbaidu = $_GET["idbaidu"];
$idjifeng = $_GET["idjifeng"];
$idanzhi = $_GET["idanzhi"];

$randomnum = $_GET["randomnum"];

if($randomnum != "123")
{
	return "";
}

return getappdata($id360,$idwandoujia,$idbaidu,$idjifeng,$idanzhi);


function getappdata($id360,$idwandoujia,$idbaidu,$idjifeng,$idanzhi)
{
	$result_arr = array();
	if($id360 != "")
	{
		$result_arr["360"] = decodedata360($id360);
	}
	if($idwandoujia != "")
	{
		$result_arr["wandoujia"] = decodedatawandoujia($idwandoujia);
	}
	if($idbaidu != "")
	{
		$result_arr["baidu"] = decodedatabaidu($idbaidu);
	}
	if($idjifeng != "")
	{
		$result_arr["jifeng"] = decodedatajifeng($idjifeng);
	}
	if($idanzhi != "")
	{
		$result_arr["anzhi"] = decodedataanzhi($idanzhi);
	}
	
//	$exit_apps_arr = get_exit_apps();
	
	
	echo  json_encode($result_arr);
}


function decodedata360($id360)
{
	$packagename = getpackagename360($id360);
	$wapurl = WAPURLHEADER360.$packagename;
	$appdetail_info_arr = getappdetailinfo360($packagename,$wapurl,$id360);
 	insert_appinfo_ifnotexit_todb($appdetail_info_arr,1);
	return $appdetail_info_arr;
}

function getpackagename360($app_id)
{
	$url = "http://zhushou.360.cn/detail/index/soft_id/$app_id";
	$htmlcontent = file_get_contents($url);
	
	$pos = strpos($htmlcontent, "downurl':")+10;
	if(!($pos === false))
	{
		$htmlcontent = substr($htmlcontent,$pos,(strlen($htmlcontent)-$pos));
	}
	$pos = strpos($htmlcontent, ",");
	if(!($pos === false))
	{
		$htmlcontent = substr($htmlcontent,0,$pos);
	}
		
	while(1)
	{
		if(strpos($htmlcontent, "/") === false)
		{
			break;
		}
		$pos = strpos($htmlcontent, "/")+1;
		$htmlcontent = substr($htmlcontent,$pos,(strlen($htmlcontent)-$pos));
	}
	
	$pos = strpos($htmlcontent, "_");
	if(!($pos === false))
	{
		$htmlcontent = substr($htmlcontent,0,$pos);
	}

	return $htmlcontent;
}

function decodedatawandoujia($idwandoujia)
{
	$wapurl_footer = ".html";
	$wapurl = WAPURLHEADERWANDOUJIA.$idwandoujia.WAPURLFOOTERWANDOUJIA;
	
	$appdetail_info_arr = getappdetailinfowandoujia($idwandoujia,$wapurl);
	insert_appinfo_ifnotexit_todb($appdetail_info_arr,2);
	return $appdetail_info_arr;
}

function decodedatabaidu($idbaidu)
{
	$wapurl_footer = ".html";
	$wapurl = WAPURLHEADERBAIDU.$idbaidu.WAPURLFOOTERBAIDU;
	
	$appdetail_info_arr = getappdetailinfobaidu($idbaidu,$wapurl);
	insert_appinfo_ifnotexit_todb($appdetail_info_arr,4);
	return $appdetail_info_arr;
}

function decodedataanzhi($idanzhi)
{
	$wapurl_footer = ".html";
	$wapurl = WAPURLHEADERANZHI.$idanzhi.$wapurl_footer;
	
	$appdetail_info_arr = getappdetailinfoanzhi($idanzhi,$wapurl);
	insert_appinfo_ifnotexit_todb($appdetail_info_arr,5);
	return $appdetail_info_arr;
}

function decodedatajifeng($idjifeng)
{
	$wapurl_footer = "/";
	$wapurl = WAPURLHEADERGFAN.$idjifeng.$wapurl_footer;
	$appdetail_info_arr = getappdetailinfojifeng($idjifeng,$wapurl);
	insert_appinfo_ifnotexit_todb($appdetail_info_arr,3);
	return $appdetail_info_arr;
}


function decodeurlandgetdata($catchtype,$appbigtype,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();
	
	$app_today_data = get_today_data();
	$app_today_score = get_today_score();
	
	for($i=0;;)
	{
		
		$apps_url_app_all_list = $urlheader.$i.".html";
		
//		本次为空，结束
//		if((count($appinfoarr) == 0) || ($appinfoarr == ""))
//		{
//			break;
//		}
		
		//有内容，解析
		
		$rankpos = ($i) * ONCECATCHCOUNT;
		$app_info_arr_insertdb_arr = catchinfodata($catchtype,$appbigtype,$rankpos,$apps_url_app_all_list,$i);
		
		if((count($app_info_arr_insertdb_arr) == 0) || ($app_info_arr_insertdb_arr == ""))
		{
			break;
		}
		
//		var_dump($app_info_arr_insertdb_arr);
//		continue;

		$app_info_arr_insertdb_data = array();
		$app_info_arr_insertdb_scoredata = array();
		//写数据库
		foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
		{
			//判断是否需要更新信息库
			$searchinfoid = searchisexit($app_info_arr_insertdb, $app_exit_infos);
			if($searchinfoid == 0)
			{
				$app_info_arr_insertdb['app_id'] = insert_appinfo_ifnotexit_todb($app_info_arr_insertdb);
				$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
				
				//重新获取存在app信息，因为有了更新
				$app_exit_infos = get_exit_appinfo_fromdb();
				$app_exit_versions = get_exit_appversion_fromdb();
			}
			else 
			{
				$app_info_arr_insertdb['app_id'] = $searchinfoid;
				$searchversionid = searchisexitversion($app_info_arr_insertdb, $app_exit_versions);
				if($searchversionid == 0)
				{
					$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
					
					//重新获取存在app信息，因为有了更新
					$app_exit_versions = get_exit_appversion_fromdb();
				}
				else 
				{
					$app_info_arr_insertdb['app_version_id'] = $searchversionid;
				}
			}
			
			//判断当天没有抓过，再写入
			if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
			{
				//update_appinfo_todb($app_info_arr_insertdb);
				//$app_today_data = get_today_data();
				$app_info_arr_insertdb_data[] = $app_info_arr_insertdb;
			}
			if(searchisexittodayscore($app_info_arr_insertdb, $app_today_score) == 0)
			{
				//update_appscore_todb($app_info_arr_insertdb);
				//$app_today_score = get_today_score();
				$app_info_arr_insertdb_scoredata[] = $app_info_arr_insertdb;
			}
		}
		
		update_appinfo_todb($app_info_arr_insertdb_data);
		update_appscore_todb($app_info_arr_insertdb_scoredata);
		$app_today_data = get_today_data();
		$app_today_score = get_today_score();
		$app_info_arr_insertdb_data = array();
		$app_info_arr_insertdb_scoredata = array();
		
		$i++;
		
	//	结束
	//	if($obj->total < ONCECATCHCOUNT)
	//	{
	//		break;
	//	}
	}
}


function searchisexit($app_infos, $app_exit_info) 
{
	foreach ($app_exit_info as $app_exit) 
	{
		if ($app_infos['packagename'] == $app_exit["AppPackageName"]) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}

function searchisexitversion($app_infos, $app_exit_version) 
{
	foreach ($app_exit_version as $app_exit) 
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]) && ($app_infos['Versioncode'] == $app_exit["versionCode"]) ) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}

function searchisexittodaydata($app_infos, $app_today_data) 
{
	foreach ($app_today_data as $app_exit) 
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]) && ($app_infos['AppRanking'] == $app_exit["rank"]) && ($app_infos['AppRankingType'] == $app_exit["ranktype"]))
		{
			return $app_exit['id'];
		}
	}
	return 0;
}
function searchisexittodayscore($app_infos, $app_today_score) 
{
	foreach ($app_today_score as $app_exit)
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]))
		{
			return $app_exit['id'];
		}
	}
	return 0;
}


//排名分类，app大类，app信息
function catchinfodata($catchtype,$appbigtype,$flag,$apps_url_app_all_list,$pageno)
{
	$app_url_arr = array();
	
	$ranking = $flag ;//* ONCECATCHCOUNT;
	$app_info_insertdb_arr = array();
	
	// 新建一个Dom实例 
	$html = new simple_html_dom();
	// 从url中加载
	@$html = file_get_html($apps_url_app_all_list);
	if($html == "")
	{
		@$html = file_get_html($apps_url_app_all_list);
		if($html == "")
		{
			return $app_info_insertdb_arr;
		}
	}
	
	
	if($pageno == 0)
	{
		$appinfo_arr = $html->find('ul.app-items-list li');
	}
	else 
	{
		$appinfo_arr = $html->find('li');
	}

	foreach($appinfo_arr as $appurl) 
	{
		$ranking++;
		$app_info_insertdb = array();

		$appinfotemp = $appurl->find('div.itmes-icon img',0);
		$app_info_insertdb['AppLogo'] = $appinfotemp->src;
		
		
		$packagename = $app_info_insertdb['AppLogo'];
//		$pos = strpos($packagename, "com.");
		
		while (1)
		{
			
			if(strpos($packagename, "/") === false)
			{
				break;
			}
			$pos = strpos($packagename, "/")+1;
			$packagename = substr($packagename,$pos,(strlen($packagename)-$pos));
		}
		
		
		
		$pos = strpos($packagename, "_");
//		if($pos > 0)
		{
			$packagename = substr($packagename,0,$pos);
		}
		$app_info_insertdb['packagename'] = $packagename;

		$appinfotemp = $appurl->find('div.items-info',0);
		$app_info_insertdb['name'] = addslashes($appinfotemp->find('h2',0)->plaintext);
		
		$rate_star_arr = $appinfotemp->find('div.items-score img');
		$rate_score = 0;
		foreach ($rate_star_arr as $rate_star)
		{
			if($rate_star->src == "/images/star_01.png")
			{
				$rate_score = $rate_score + 1;
			}
			else if($rate_star->src == "/images/star_02.png")
			{
				$rate_score = $rate_score + 0.5;
			}
		}
		$app_info_insertdb['Rating'] = $rate_score;
		
		
		$wapurl_footer = $appurl->find('a.clickarea',0)->href;
		$wapurl = WAPURLHEADER.$wapurl_footer;
		$appdetail_info_arr = getappdetailinfo($wapurl);
		
		$app_info_insertdb['AppID'] = str_replace("/info_","",$wapurl_footer);
		$app_info_insertdb['AppID'] = str_replace(".html","",$app_info_insertdb['AppID']);
		$app_info_insertdb['AppID'] = str_replace(" ","",$app_info_insertdb['AppID']);
		$app_info_insertdb['url'] = WEBURLHEADERANZHI.$app_info_insertdb['AppID'].".html";
		
		
		$app_info_insertdb['AppDownTimes'] = $appdetail_info_arr['AppDownTimes'];
		$app_info_insertdb['AppVersion'] = $appdetail_info_arr['AppVersion'];
		$app_info_insertdb['Versioncode'] = "";
		$app_info_insertdb['AppSize'] = $appdetail_info_arr['AppSize'];
		
		$app_info_insertdb['ScoreType'] = "";
		
		$app_info_insertdb['AppType'] = "";
//		if($catchtype <= 100)
		{
			$app_info_insertdb['AppType'] = $catchtype;
		}
		$app_info_insertdb['AppBigType'] = $appbigtype;
		$app_info_insertdb['AppRanking'] = $ranking;
		$app_info_insertdb['AppRankingType'] = $catchtype;
		
		$app_info_insertdb_arr[] = $app_info_insertdb;
	}
	//var_dump($app_info_insertdb_arr);die();
	$html->clear();
	
	return $app_info_insertdb_arr;
}




function getappdetailinfo360($packagename,$app_detail_url,$id360)
{
	$appdetailinfo_arr = array();
	@$json_string = file_get_html($app_detail_url);
	$obj=json_decode($json_string);
	
	if(($obj->errno != 0) || ($obj->total != 1))
	{
		return $appdetailinfo_arr;
	}
	
	$appinfoarr_temp = $obj->data;
	if(($appinfoarr_temp == "") || (count($appinfoarr_temp) == 0) || ($appinfoarr_temp[0] == "") || (count($appinfoarr_temp[0]) == 0))
	{
		return $appdetailinfo_arr;
	}
	$appinfo = $appinfoarr_temp[0];

	$appdetailinfo_arr['name'] = addslashes($appinfo->name);
	$appdetailinfo_arr['packagename'] = $packagename;
	$appdetailinfo_arr['AppDownTimes'] = getdownloadtimes($appinfo->download_times);
	$appdetailinfo_arr['AppID'] = $id360;
	$appdetailinfo_arr['Rating'] = $appinfo->rating;
	$appdetailinfo_arr['AppVersion'] = $appinfo->version_name;
	$appdetailinfo_arr['Versioncode'] = $appinfo->version_code;
	$appdetailinfo_arr['AppLogo'] = $appinfo->logo_url;
	$appdetailinfo_arr['AppSize'] = $appinfo->size;
	
	$appdetailinfo_arr['AppType'] = 10000;
	$appdetailinfo_arr['AppBigType'] = 10000;
	$appdetailinfo_arr['AppRanking'] = 1;
	$appdetailinfo_arr['AppRankingType'] = 10000;
	return $appdetailinfo_arr;
}


function getappdetailinfowandoujia($idwandoujia,$app_detail_url)
{
	$appdetailinfo_arr = array();
	@$json_string = file_get_html($app_detail_url);
	$appinfo_data = json_decode($json_string);
	//本次为空，结束
	if((count($appinfo_data) == 0) || ($appinfo_data == ""))
	{
		return $appdetailinfo_arr;
	}

	$appdetailinfo_arr['name'] = addslashes($appinfo_data->title);
	$appdetailinfo_arr['packagename'] = $appinfo_data->packageName;
	$appdetailinfo_arr['AppDownTimes'] = getdownloadtimes($appinfo_data->installedCountStr);
	$appdetailinfo_arr['ScoreType'] = $appinfo_data->likesRate;//%的人喜欢
	$appdetailinfo_arr['Rating'] = @"";//$appinfo->rating;
	$appdetailinfo_arr['AppVersion'] = $appinfo_data->apks[0]->versionName;
	$appdetailinfo_arr['Versioncode'] = $appinfo_data->apks[0]->versionCode;
	$appdetailinfo_arr['AppBaikeName'] = @"";//$appinfo->baike_name;
	$appdetailinfo_arr['AppLogo'] = $appinfo_data->icons->px78;
	$appdetailinfo_arr['AppSize'] = $appinfo_data->apks[0]->bytes;
	$appdetailinfo_arr['CommentsCount'] = $appinfo_data->commentsCount;
	
	$appdetailinfo_arr['AppType'] = 10000;
	$appdetailinfo_arr['AppBigType'] = 10000;
	$appdetailinfo_arr['AppRanking'] = 0;
	$appdetailinfo_arr['AppRankingType'] = 10000;
	
	return $appdetailinfo_arr;
}


function getappdetailinfojifeng($idjifeng,$app_detail_url)
{
	$appdetailinfo_arr = array();
	@$json_string = file_get_contents($app_detail_url);
    $json_str = $json_string;
	$json_str = substr($json_str, 0, -1);
	$json_str = substr($json_str, 1, strlen($json_str));
	$appinfo = json_decode($json_str);

	if(($appinfo == "") || (count($appinfo) == 0))
	{
		return $appdetailinfo_arr;
	}
	
	$appdetailinfo_arr['name'] = addslashes($appinfo->name);
	$appdetailinfo_arr['packagename'] = $appinfo->packagename;
	$appdetailinfo_arr['AppDownTimes'] = getdownloadtimes($appinfo->download_count);
	$appdetailinfo_arr['AppID'] = $idjifeng;
	$appdetailinfo_arr['Rating'] = $appinfo->rating;
	$appdetailinfo_arr['AppVersion'] = $appinfo->version_name;
	$appdetailinfo_arr['Versioncode'] = $appinfo->version_code;
	$appdetailinfo_arr['AppLogo'] = $appinfo->icon_url;
	$appdetailinfo_arr['AppSize'] = $appinfo->app_size;
	
	$appdetailinfo_arr['AppType'] = 10000;
	$appdetailinfo_arr['AppBigType'] = 10000;
	$appdetailinfo_arr['AppRanking'] = 0;
	$appdetailinfo_arr['AppRankingType'] = 10000;
	$appdetailinfo_arr['url'] = WEBURLHEADERGFAN.$appdetailinfo_arr['AppID'].".html";

	return $appdetailinfo_arr;
}
 
function getappdetailinfobaidu($idbaidu,$app_detail_url)
{
	$appdetailinfo_arr = array();
	
	// 新建一个Dom实例 
	$html_detail = new simple_html_dom();
	// 从url中加载 
	@$html_detail = file_get_html($app_detail_url);
	if($html_detail == "")
	{
		@$html_detail = file_get_html($app_detail_url);
		if($html_detail == "")
		{
			return $appdetailinfo_arr;
		}
	}
	
	$appinfo_temp = $html_detail->find('div.info-top dl.clearfix',0);
	$appdetailinfo_arr['AppLogo'] = $appinfo_temp->find('dt.fleft img[id=app-logo]',0)->src;
	//解析属性
	$appinfo_temp_param = $appinfo_temp->find('dd.info-params table tr',0);
	$appdetailinfo_arr['AppVersion'] = $appinfo_temp_param->find('span.params-vname',0)->plaintext;
	$appdetailinfo_arr['Versioncode'] = "";
	$appdetailinfo_arr['packagename'] = "";
	$appdetailinfo_arr['AppID'] = $idbaidu;
	$appdetailinfo_arr['AppSize'] = $appinfo_temp_param->find('span.params-size',0)->plaintext;
	$appdetailinfo_arr['url'] = WEBURLHEADERBAIDU.$app_info_insertdb['AppID'];
	
	$appdetailinfo_arr['AppDownTimes'] = $appinfo_temp->find('dd.info-params table tr',1)->find('span.params-download-num',0)->plaintext;
	if(strstr($appdetailinfo_arr['AppDownTimes'],"+"))
	{
		$appdetailinfo_arr['AppDownTimes'] = str_replace("+","",$appdetailinfo_arr['AppDownTimes']);
		$appdetailinfo_arr['AppDownTimes'] = str_replace(" ","",$appdetailinfo_arr['AppDownTimes']);
	}
	$appdetailinfo_arr['AppDownTimes'] = getdownloadtimes($appdetailinfo_arr['AppDownTimes']);
	
	$appdetailinfo_arr['AppType'] = 10000;
	$appdetailinfo_arr['AppBigType'] = 10000;
	$appdetailinfo_arr['AppRanking'] = 0;
	$appdetailinfo_arr['AppRankingType'] = 10000;

	$appname = $html_detail->find('div.info-top dl.clearfix span[id=appname]',0)->plaintext;
	$appdetailinfo_arr['name'] = addslashes($appname);

	$scoredata = $html_detail->find('div.info-top dl.clearfix dd.info-star b.color-orange',0)->plaintext;
	$scoredata = str_replace("分","",$scoredata);
	$scoredata = str_replace(" ","",$scoredata);
	$appdetailinfo_arr['Rating'] = $scoredata;
	
	//评分人数
	$scoredata = $html_detail->find('div.info-top dl.clearfix dd.info-star span.color-gray',0)->plaintext;
	$scoredata = str_replace("已有","",$scoredata);
	$scoredata = str_replace("人评分","",$scoredata);
	$scoredata = str_replace("(","",$scoredata);
	$scoredata = str_replace(")","",$scoredata);
	$scoredata = str_replace(" ","",$scoredata);
	$appdetailinfo_arr['commentcount'] = $scoredata;
	
	$html_detail->clear();
	return $appdetailinfo_arr;
}

function getappdetailinfoanzhi($idanzhi,$app_detail_url)
{
	$appdetailinfo_arr = array();

	// 新建一个Dom实例 
	$html_detail = new simple_html_dom();
	// 从url中加载
	$html_detail = "";
	@$html_detail = file_get_html($app_detail_url);
	if($html_detail == "")
	{
		@$html_detail = file_get_html($app_detail_url);
		if($html_detail == "")
		{
			return $appdetailinfo_arr;
		}
	}

	$appdetailinfo = $html_detail->find('div.app_header div.app_info img.app_img',0);
	$appdetailinfo_arr['AppLogo'] = $appdetailinfo->src;

	$appdetailinfo = $html_detail->find('div.app_header div.app_info div.app_txt h3',0);
	$appdetailinfo_arr['name'] = addslashes($appdetailinfo->plaintext);
	
	$rate_star_arr = $html_detail->find('div.app_header div.app_info div.app_txt div.app_score img');
	$rate_score = 0;
	foreach ($rate_star_arr as $rate_star)
	{
		if($rate_star->src == "/images/star_01.png")
		{
			$rate_score = $rate_score + 1;
		}
		else if($rate_star->src == "/images/star_02.png")
		{
			$rate_score = $rate_score + 0.5;
		}
	}
	$appdetailinfo_arr['Rating'] = $rate_score;
	
	$appdetailinfo_arr['packagename'] = "";
	$appdetailinfo_arr['AppID'] = $idanzhi;
	$appdetailinfo_arr['url'] = WEBURLHEADERANZHI.$app_info_insertdb['AppID'].".html";
	
	$appdetailinfo_arr['ScoreType'] = "";
	$appdetailinfo_arr['AppType'] = 10000;
	$appdetailinfo_arr['AppBigType'] = 10000;
	$appdetailinfo_arr['AppRanking'] = 0;
	$appdetailinfo_arr['AppRankingType'] = 10000;
	
	$appdetailinfo = $html_detail->find('div.app_header ul li');
	
	$appdetailinfo_arr['AppDownTimes'] = 0;
	$appdetailinfo_arr['AppSize'] = "";
	$appdetailinfo_arr['AppVersion'] = "1.0";
	
	if(strstr($appdetailinfo[0]->plaintext,"下载：大于"))
	{
		$appdetailinfo_arr['AppDownTimes'] = str_replace("下载：大于","",$appdetailinfo[0]->plaintext);
		$appdetailinfo_arr['AppDownTimes'] = str_replace("次","",$appdetailinfo_arr['AppDownTimes']);
		$appdetailinfo_arr['AppDownTimes'] = str_replace(" ","",$appdetailinfo_arr['AppDownTimes']);
	}
	else if(strstr($appdetailinfo[0]->plaintext,"版本"))
	{
		$appdetailinfo_arr['AppVersion'] = str_replace("版本：","",$appdetailinfo[0]->plaintext);
		$appdetailinfo_arr['AppVersion'] = str_replace(" ","",$appdetailinfo_arr['AppVersion']);
	}
	else if(strstr($appdetailinfo[0]->plaintext,"大小"))
	{
		$appdetailinfo_arr['AppSize'] = str_replace("大小：","",$appdetailinfo[0]->plaintext);
		$appdetailinfo_arr['AppSize'] = str_replace(" ","",$appdetailinfo_arr['AppSize']);
	}
	
	if(strstr($appdetailinfo[1]->plaintext,"下载"))
	{
		$appdetailinfo_arr['AppDownTimes'] = str_replace("下载：大于","",$appdetailinfo[1]->plaintext);
		$appdetailinfo_arr['AppDownTimes'] = str_replace("次","",$appdetailinfo_arr['AppDownTimes']);
		$appdetailinfo_arr['AppDownTimes'] = str_replace(" ","",$appdetailinfo_arr['AppDownTimes']);
	}
	else if(strstr($appdetailinfo[1]->plaintext,"版本"))
	{
		$appdetailinfo_arr['AppVersion'] = str_replace("版本：","",$appdetailinfo[1]->plaintext);
		$appdetailinfo_arr['AppVersion'] = str_replace(" ","",$appdetailinfo_arr['AppVersion']);
	}
	else if(strstr($appdetailinfo[1]->plaintext,"大小"))
	{
		$appdetailinfo_arr['AppSize'] = str_replace("大小：","",$appdetailinfo[1]->plaintext);
		$appdetailinfo_arr['AppSize'] = str_replace(" ","",$appdetailinfo_arr['AppSize']);
	}
	
	if(strstr($appdetailinfo[2]->plaintext,"下载"))
	{
		$appdetailinfo_arr['AppDownTimes'] = str_replace("下载：大于","",$appdetailinfo[2]->plaintext);
		$appdetailinfo_arr['AppDownTimes'] = str_replace("次","",$appdetailinfo_arr['AppDownTimes']);
		$appdetailinfo_arr['AppDownTimes'] = str_replace(" ","",$appdetailinfo_arr['AppDownTimes']);
	}
	else if(strstr($appdetailinfo[2]->plaintext,"版本"))
	{
		$appdetailinfo_arr['AppVersion'] = str_replace("版本：","",$appdetailinfo[2]->plaintext);
		$appdetailinfo_arr['AppVersion'] = str_replace(" ","",$appdetailinfo_arr['AppVersion']);
	}
	else if(strstr($appdetailinfo[2]->plaintext,"大小"))
	{
		$appdetailinfo_arr['AppSize'] = str_replace("大小：","",$appdetailinfo[2]->plaintext);
		$appdetailinfo_arr['AppSize'] = str_replace(" ","",$appdetailinfo_arr['AppSize']);
	}
	
	if(strstr($appdetailinfo[3]->plaintext,"下载"))
	{
		$appdetailinfo_arr['AppDownTimes'] = str_replace("下载：大于","",$appdetailinfo[3]->plaintext);
		$appdetailinfo_arr['AppDownTimes'] = str_replace("次","",$appdetailinfo_arr['AppDownTimes']);
		$appdetailinfo_arr['AppDownTimes'] = str_replace(" ","",$appdetailinfo_arr['AppDownTimes']);
	}
	else if(strstr($appdetailinfo[3]->plaintext,"版本"))
	{
		$appdetailinfo_arr['AppVersion'] = str_replace("版本：","",$appdetailinfo[3]->plaintext);
		$appdetailinfo_arr['AppVersion'] = str_replace(" ","",$appdetailinfo_arr['AppVersion']);
	}
	else if(strstr($appdetailinfo[3]->plaintext,"大小"))
	{
		$appdetailinfo_arr['AppSize'] = str_replace("大小：","",$appdetailinfo[3]->plaintext);
		$appdetailinfo_arr['AppSize'] = str_replace(" ","",$appdetailinfo_arr['AppSize']);
	}
	$appdetailinfo_arr['AppDownTimes'] = getdownloadtimes($appdetailinfo_arr['AppDownTimes']);
	
	$html_detail->clear();
	return $appdetailinfo_arr;
}


function getdownloadtimes($downloadtimes)
{
	if(strstr($downloadtimes,"亿"))
	{
		$downloadtimes = str_replace("亿","",$downloadtimes) * 100000000;
	}
	if(strstr($downloadtimes,"万"))
	{
		$downloadtimes = str_replace("万","",$downloadtimes) * 10000;
	}
	if(strstr($downloadtimes,"千"))
	{
		$downloadtimes = str_replace("千","",$downloadtimes) * 1000;
	}
	if(strstr($downloadtimes,"百"))
	{
		$downloadtimes = str_replace("百","",$downloadtimes) * 100;
	}
	return $downloadtimes;
}



function get_exit_apps()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$today = date("Y-m-d");

	$sql = 'select id, app_id,market_id,packageName from jk_apps_info_360 where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
	  	$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info_temp['market_id'] = $row['market_id'];
	  	$app_exit_info_temp['packageName'] = $row['packageName'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}

//app基础信息
function insert_appinfo_ifnotexit_todb($app_info,$markt_id)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'insert into jk_apps_packagenamewithid (app_id,market_id,packageName,url)
	 values ('.$app_info['AppID'].','.$markt_id.',"'.$app_info['packagename'].'","'.$app_info['url'].'")'; 

	$rs = mysql_query($sql, $link);
	//echo $sql;die();
	$filename_success = LOGPATHHEADER.'logs/catappdata_success.log';
	$filename_fail = LOGPATHHEADER.'logs/catappdata_info_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/catappdata_info_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["packagename"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["packagename"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
	return mysql_insert_id();
}

?>